﻿Imports System.Windows.Forms
Imports System.Data.OleDb



Public Class qrySave

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If txtUsr.Text = "" Then 'If UserName textbox is empty....
            MsgBox("Specify a username first", MsgBoxStyle.Exclamation) 'Notify user that this is not allowed
            Return 'and stop NOW!
        End If
        If qryDesc.Text = "" Then 'If description box is empty
            MsgBox("Description field cannot be empty", MsgBoxStyle.Exclamation) 'Notify user that this is not right
            Return 'And stop immidiately
        End If
        SaveQuery() 'Call SaveQuery SubRoutine

        Me.Close() 'Close Dialog after everything worked ok
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub qrySave_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub
    Private Sub SaveQuery()

        Dim st As String = "select * from RecFinder_Log" 'Statement to fill Dataset
        Dim dst As New DataSet 'The Dataset for getting - saving logs
        Dim conn As New OleDbConnection
        If My.Settings.RegistryAccessible = True Then 'If Registry can be accessed
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\RecFinder\recfinder_connString", "path", Nothing) 'take the path from registry
            Main.pathInUse.Text = "Using : Registry path (Registry Active)" '<-- FIX , update status bar in main form
        Else
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Main.defaultPath 'else take the path from application's variables
            Main.pathInUse.Text = "Using : Default Path (Registry is disabled)" '<--- FIX , update status bar in main form
        End If


        Try 'Try....

            Using adapter As New OleDbDataAdapter(st, conn) 'Declare adapter for the job
                adapter.Fill(dst, "RecFinder_Log") 'Fill the dataset....

                Dim commandBuilder As New OleDbCommandBuilder(adapter) 'This Component will execute INSERT Statement inside the dataset
                Dim newRecord As DataRow = dst.Tables("RecFinder_Log").NewRow() 'Create a new row to the dataset table

                newRecord.Item("Dbase") = Main.filepath 'Field Dbase gets Database name (To be exact, the full path to db file
                newRecord.Item("DbTable") = Main.currentTable 'DbTable gets the Current Table Name
                newRecord.Item("UserQuery") = Main.QueryBox.Text 'UserQuery get everything inside the query box (i.e. the query)
                newRecord.Item("Descr") = qryDesc.Text 'This field gets the Description from the dialogbox
                newRecord.Item("SaveDateTime") = Now 'This Field get the current TimeStamp
                newRecord.Item("UserName") = txtUsr.Text 'Finally this is the username we specify in dialog box


                dst.Tables("RecFinder_Log").Rows.Add(newRecord) 'Add this new row to the dataset
                adapter.Update(dst, "RecFinder_Log") 'Send changes to database
                MsgBox("Query Saved", MsgBoxStyle.Information) 'Notify user that everything worked fine

            End Using

        Catch ex As Exception 'if something goes wrong...
            MsgBox("An Error Occured during saving the query to database", MsgBoxStyle.Critical) 'Notify User....
        End Try


    End Sub
End Class
